package me.khrystal.rxnews.module.news.model;

import com.socks.library.KLog;

import java.util.List;
import java.util.Map;

import me.khrystal.rxnews.bean.NeteastNewsSummary;
import me.khrystal.rxnews.callback.RequestCallback;
import me.khrystal.rxnews.http.Api;
import me.khrystal.rxnews.http.HostType;
import me.khrystal.rxnews.http.manager.RetrofitManager;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.Func2;

/**
 * usage:
 * author: kHRYSTAL
 * create time: 16/4/22
 * update time:
 * email: 723526676@qq.com
 */
public class INewsListInteractorImpl implements INewsListInteractor<List<NeteastNewsSummary>>{

    @Override
    public Subscription requestNewsList(final RequestCallback<List<NeteastNewsSummary>> callback, String type, final String id, int startPage) {
        KLog.e("新闻列表:" + type + ";"+id);
        return RetrofitManager.getInstance(HostType.NETEASE_NEWS_VIDEO)
                .getNewsListObservable(type,id,startPage).doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        callback.beforeRequest();
                    }
                }).subscribeOn(AndroidSchedulers.mainThread())
                .doOnError(new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        KLog.e("错误时处理：" + throwable + "---" + throwable.getLocalizedMessage());
                    }
                    //flatMap 支持一个Observable作为输入 一个Observable作为输出
                    //<I,O> I为输入 O为输出
                }).flatMap(new Func1<Map<String, List<NeteastNewsSummary>>, Observable<NeteastNewsSummary>>() {
                    @Override
                    public Observable<NeteastNewsSummary> call(Map<String, List<NeteastNewsSummary>> map) {
                        //Observable.from()支持一个集合作为输入 每次输出集合中的一个对象
                        if (id.equals(Api.HOUSE_ID))
                            return Observable.from(map.get("北京"));
                        return Observable.from(map.get(id));
                    }
                })
                .toSortedList(new Func2<NeteastNewsSummary, NeteastNewsSummary, Integer>() {
                    //按时间先后排序
                    @Override
                    public Integer call(NeteastNewsSummary neteastNewsSummary, NeteastNewsSummary neteastNewsSummary2) {
                        return neteastNewsSummary2.ptime.compareTo(neteastNewsSummary.ptime);
                    }
                }).subscribe(new Subscriber<List<NeteastNewsSummary>>() {
                    @Override
                    public void onCompleted() {
                        callback.requestComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.requestError(e.getLocalizedMessage() + "\n" +e);
                    }

                    @Override
                    public void onNext(List<NeteastNewsSummary> neteastNewsSummaries) {
                        callback.requestSuccess(neteastNewsSummaries);
                    }
                });
    }
}
